<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in firstList" :key="item.id" class="f-item b-b" :class="{active: item.id == currentId}" @click="tabtap(item)">
				{{item.name}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
			
				<view v-for="item2 in secondList" :key="item2.id" class="s-list" :id="'main-'+item2.id">
					<text class="s-item" :class="{active: item2.pid == currentId}">{{item2.name}}</text>
					<view class="t-list">
						<view @click="navToList(item2.id, item3.id)" v-if="item3.pid === item2.id" class="t-item" v-for="item3 in item2.items" :key="item3.id">
							<text>{{item3.name}}</text>
						</view>
					</view>
				</view>

			
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId:1,
				firstList: [],
				secondList:[],
			}
		},
		onLoad(){
			var categoryUrl=this.requestUrl.category;
			var self=this;
			this.util.request(categoryUrl,'post',function(res){
				if(res.data.code==0 && res.data.data){
					// 一级分类 self.firstList
					self.firstList=res.data.data;
					let fList=self.firstList;
					fList.forEach(item=>{
						item.items.forEach(item2=>{
							self.secondList.push(item2)
						})
					})
					self.$forceUpdate();
					
				}
			});
			
		},
		onReady(){
			
		},
		methods: {
			//一级分类点击
			tabtap(item){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				this.currentId = item.id;
				let index = this.secondList.findIndex(sitem=>sitem.pid === item.id);
				this.$nextTick(()=>{
					this.tabScrollTop = this.secondList[index].top;
				})
				this.$forceUpdate();
			},
			//右侧栏滚动
			asideScroll(e){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.secondList.filter(item=>item.top <= scrollTop).reverse();
				if(tabs.length > 0){
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize(){
				let h = 0;
				this.secondList.forEach(item=>{
					let view = uni.createSelectorQuery().in(this).select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h= h+data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(sid, tid){
				uni.navigateTo({
					url: `/pages/product/list?fid=${this.currentId}&sid=${sid}&tid=${tid}`
				})
			}
		},
		watch:{
			firstList:function(){
				// 二级分类 self.secondList
				// let fList=this.firstList;
				// fList.forEach(item=>{
				// 	item.items.forEach(item2=>{
				// 		this.secondList.push(item2)
				// 	})
				// })
			}
		}
	}
</script>

<style lang='scss'>
	
	page,
	.content {
		height: 100%;
		background-color: $uni-bg-color-grey;
	}

	.content {
		display: flex;
	}
	.left-aside {
		flex-shrink: 0;
		width: 200upx;
		height: 100%;
		background-color:$uni-bg-color;
	}
	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: $uni-text-color;
		position: relative;
		&.active{
			color: $uni-text-color-zhu;
			background:$uni-bg-color-grey;
			&:before{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 8upx;
				background-color: $uni-bg-color-zhu;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside{
		flex: 1;
		overflow: hidden;
		padding-left: 20upx;
	}
	.s-item{
		display: flex;
		align-items: center;
		height: 70upx;
		padding-top: 8upx;
		font-size: $uni-font-size-base;
		color: $uni-text-color;
	}
	.t-list{
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		background: #fff;
		padding: 20upx;
		box-sizing: border-box;
		&:after{
			content: '';
			flex: 99;
			height: 0;
		}
	}
	.t-item{
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		font-size:$uni-font-size-base;
		color: $uni-text-color;
		padding-bottom: 20upx;
		padding:5rpx 10rpx;
		margin:10rpx;
		border:solid 1px $uni-border-color;
		border-radius:$uni-border-radius-lg;
	}
	.active{color:$uni-text-color-zhu;}
</style>
